package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.StQuestion;
import com.example.demo.entity.StQuestionOption;
import com.example.demo.entity.request.StQuestionOptionRequest;
import com.example.demo.entity.request.StQuestionRequest;
import com.example.demo.entity.response.StQuestionResponse;
import com.example.demo.mapper.StQuestionMapper;
import com.example.demo.mapper.StQuestionOptionMapper;
import com.example.demo.service.StQuestionOptionService;
import com.example.demo.service.StQuestionService;
import com.example.demo.utils.DateTransferUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/question")
public class StQuestionController extends BaseController {

    @Resource
    StQuestionOptionMapper optionMapper;
    @Autowired
    StQuestionService questionService;
    @Autowired
    StQuestionOptionService optionService;

    @ApiOperation(value = "新增/修改题目")
    @PostMapping("/save")
    public Result<?> save(@RequestBody StQuestionRequest request) {
        if (StrUtil.isEmpty(request.getType().toString()))
            return Result.error("401", "咋回事，题目类型也不写");
        StQuestion question = new StQuestion();
        BeanUtils.copyProperties(request, question);
        question.setDeleted(false);

        if (request.getType() == 2 && request.getCorrectList().size() > 0) {
            question.setCorrectList(questionService.parseListToStr2(request.getCorrectList()));
        }

        if (question.getId() == null) {
            question.setCreateUser(getUser().getPeopleName());
            question.setCreateTime(DateTransferUtil.getNow());
        } else {
            question.setModifyUser(getUser().getPeopleName());
            question.setModifyTime(DateTransferUtil.getNow());
        }
        questionService.saveOrUpdate(question);

        if (question.getType() == 1 || question.getType() == 2) {
            if (request.getOptions().size() == 0)
                return Result.error("401", "咋回事，选择题选项也不给");
            for (StQuestionOptionRequest item : request.getOptions()) {
                StQuestionOption option = new StQuestionOption();
                option.setQuestionId(question.getId());
                option.setPrefix(item.getPrefix());
                option.setContent(item.getContent());
                BeanUtils.copyProperties(item, option);
                optionService.saveOrUpdate(option);
            }
        }
        return Result.success();
    }

    @ApiOperation(value = "根据题目id查找题目")
    @GetMapping("/select/{id}")
    public Result<?> select(@PathVariable Integer id) {
        StQuestion question = questionService.getById(id);
        StQuestionResponse response = new StQuestionResponse();
        BeanUtils.copyProperties(question, response);
        if (StrUtil.isNotEmpty(question.getCorrectList())) {
            List<String> split= Arrays.asList(question.getCorrectList().split(","));
            response.setCorrectList(split);
        }
        List<StQuestionOption> list = optionMapper.getByQuestionId(id);
        response.setOptions(list);
        return Result.success(response);
    }


    @ApiOperation(value = "分页查询/根据题目类型")
    @GetMapping("/list")
    public Result<?> page(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam Integer type,
                          @RequestParam(defaultValue = "") String title) {
        LambdaQueryWrapper<StQuestion> wrapper = Wrappers.lambdaQuery();
        if (type != 0)
            wrapper.eq(StQuestion::getType, type);
        if (StrUtil.isNotEmpty(title))
            wrapper.like(StQuestion::getTitle, title);
        wrapper.eq(StQuestion::getDeleted, false);
        Page<StQuestion> questionPage = questionService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(questionPage);
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        StQuestion question = questionService.getById(id);
        if (question == null) return Result.error("404", "介id咋搜不到捏");
        question.setDeleted(true);
        questionService.updateById(question);
        return Result.success();
    }

    @GetMapping("/all")
    public Result<?> all() {
        LambdaQueryWrapper<StQuestion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StQuestion::getDeleted, false);
        List<StQuestion> list = questionService.list(wrapper);
        return Result.success(list);
    }
}
